$(document).ready(function() {
	console.log('in contractGrid part');
	console.log(projectObj);
	var str_urlContractGird=contextPath+'/project/detail/'+projectId+'/contractDatagrid.json';
	var array2_contractGridClumns=[[    
	        {field:'id',title:'id',width:70,checkbox:true},    //hidden:true,
	        {field:'sn',title:'编号',width:40},    
	        {field:'name',title:'合同名',width:400},        
	        {field:'date',title:'日期',width:60},        
	        {field:'partAName',title:'委托单位',width:60},        
	        //{field:'builderName',title:'建设单位',width:60},        
	        {field:'amount',title:'合同金额',width:50},        
	       /* {field:'performanceStatus',title:'履约情况',width:60},        
	        {field:'isBilling',title:'是否开票',width:60},        
	        {field:'settlementSn',title:'结清编号',width:60}, */   
	        {field : 'action',
			 title : '操作',
			 width : 75,
			 formatter : function(value, row, index) {
					var str = '';
					//详情页
					if ($.canDetailContract) {
						//str += $.formatString('<img onclick="addDesignerDetailTab(\'{0}\',\'{1}\');" src="{2}" title="详情"/>', row.id,row.name, '${pageContext.request.contextPath}/style/images/extjs_icons/book_open.png');
						str += $.formatString('<a title="合同详情" class="easyui-tooltip" href="javascript:void(0);"><span class="iconfont" onclick="addTabWithContractDetail(\'{0}\',\'{1}\');" >&#xe655;</span></a>', row.id,row.name);
					}
					str += '&nbsp;&nbsp;';
					if($.canReleaseContract){
						//str += $.formatString('<img onclick="addDesignerDetailTab(\'{0}\',\'{1}\');" src="{2}" title="详情"/>', row.id,row.name, '${pageContext.request.contextPath}/style/images/extjs_icons/book_open.png');
						str += $.formatString('<a title="解除关联" class="easyui-tooltip" href="javascript:void(0);"><span class="iconfont" onclick="releaseContract(\'{0}\');" >&#xe655;</span></a>', row.id);							
					}
					str += '&nbsp;&nbsp;';
					if ($.canDeleteContract) {
						//str += $.formatString('<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>', row.id, '${pageContext.request.contextPath}/style/images/extjs_icons/cancel.png');
						str += $.formatString('<a title="删除合同" class="easyui-tooltip" href="javascript:void(0);"><span class="iconfont" onclick="deleteContract(\'{0}\');" >&#xe655;</span></a>', row.id);							
					}
					return str;
				}
			}
	    ]];

	ez_contractGrid=$('#contract-grid').datagrid({    
	   // data: contractGridObj,  
	    url:str_urlContractGird,
	    fitColumns : true,
	    nowrap : false,
	    striped:true,
	    queryParams:{"projectId":projectObj.id},
	    columns:array2_contractGridClumns
	 });


});

//增加合同详情页
function addTabWithContractDetail(contractId,tabTitle){
	console.log(contractId);
	console.log(tabTitle);
}

function updateLinkWithContract(array_contractId){
	
			var str_urlOfUpdateLinkWithContract=contextPath+'/project/detail/updateContractLink.json';
			var str_contractIds=array_contractId.join(",");
			var postData={'projectId':projectId,"contractIds":str_contractIds};
			$.ajax({  
			    url:str_urlOfUpdateLinkWithContract,// 跳转到 action  
			    data:postData,  
			    type:'post',  
			    dataType:'json', //预期服务器返回的数据类型。 
			    beforeSend:function(XMLHttpRequest ){
			    	$.messager.progress({
						title : '提示',
						text : '正在处理~'
					});
			    },
			    success:function(obj) {
			    	console.log(obj);
			    	if(obj.success){
			    		//刷洗contract grid
			    		$('#contract-grid').datagrid('reload');
			    	}else{
			    		alert(obj.msg);
			    	}
			    	$.messager.progress('close');
			    },  
			     error : function(data) { 
			    	 //console.log(data);
			    	 $.messager.progress('close');
			    	 alert("服务器错误，请于管理员联系:18639551912。");
			    }  
			});
	
		
}


//解除合同和项目的关联
function releaseContract(str_contractId){
	$.messager.confirm('确认对话框', '请确认解除项目和合同的关联关系?', function(r){
		if(r){
			var array_contractId=getArrayOfContractId();
			console.log('before remove:' +array_contractId);
			//console.log('releaseContract('+str_contractId+')');
			array_contractId.remove(str_contractId);
			console.log('after remove:' +array_contractId);
			updateLinkWithContract(array_contractId);
		}
	});
}

//批量解除合同和项目的关联
function batchReleaseContract(){
	var array_checkedRow=$('#contract-grid').datagrid('getChecked');
	if(array_checkedRow.length==0){
		alert("未勾选项目");
		return;
	}
	$.messager.confirm('确认对话框', '请确认解除项目和合同的关联关系?', function(r){
		if(r){
			var array_contractId=getArrayOfContractId();
			console.log('before remove:' +array_contractId);
			
		
			console.log(array_checkedRow);
			var array_deleteId=new Array();
			$.each(array_checkedRow,function(){
				console.log(this);
				array_contractId.remove(this.id);
			});
			console.log('after remove:' +array_contractId);
			updateLinkWithContract(array_contractId);
		}
	});
}

//删除数据库中 对应合同 item对象 
function deleteContract(str_contractIds){
	console.log('deleteContract('+str_contractIds+')');
	var postData={'contractIds':str_contractIds};
	var str_urlDeleteContract=contextPath+'/contract/deleteByIds';
	$.messager.confirm('确认对话框', '确认彻底删除合同信息?', function(r){
		if (r){
		    // 退出操作;
			$.ajax( {  
			    url:str_urlDeleteContract,// 跳转到 action  
			    data:postData,  
			    type:'post',  
			    dataType:'json', //预期服务器返回的数据类型。 
			    beforeSend:function(XMLHttpRequest ){
			    	$.messager.progress({
						title : '提示',
						text : '正在处理~'
					});
			    },
			    success:function(obj) {
			    	console.log(obj);
			    	if(obj.success){
			    		//刷洗contract grid
			    		contractGrid.datagrid('reload');
			    	}else{
			    		alert(obj.msg);
			    	}
			    	$.messager.progress('close');
			    },  
			     error : function(data) { 
			    	 //console.log(data);
			    	 $.messager.progress('close');
			    	 alert("服务器错误，请于管理员联系:18639551912。");
			     }  
			});
		}
	});
}

//批量 删除 合同对象
function batchDeleteContract(){
	var array_row=$('#contract-grid').datagrid('getChecked');
	if(array_row.length==0){
		alert("未勾选项目");
		return;
	}
	var array_deleteId=new Array();
	$.each(array_row,function(){
		array_deleteId.push(this.id);
	});
	var str_contractIds=array_deleteId.join(',');
	deleteContract(str_contractIds);
	console.log()
}

function openDlgOfSelectContract(){
	var obj_contractData=$('#contract-grid').datagrid('getData');
	console.log('obj_contractData = ');
	console.log(obj_contractData);
	var str_contentDivId='div-contractDlg';
	var str_idPrefix='contract';
	var str_dlgTitle="text head";
	var obj_filterClass={
						'className':'contract',
						'fieldNames':['sn','name']	
						};


	/*
	url : obj_dataGridParameter.url,
	sortName : obj_dataGridParameter.sortName,
	sortOrder : obj_dataGridParameter.sortOrder, 
	columns:obj_dataGridParameter.columns
	*/
	var obj_dataGridParameter=new Object();

	obj_dataGridParameter.url=contextPath+'/common/dataGrid/ldrdSelect/contract/griddata.json';

	obj_dataGridParameter.columns=[[   
							        {field:'id',title:'id',width:70,hidden:true},    //hidden:true,
							        {field:'sn',title:'编号',width:70},    
							        {field:'name',title:'合同名',width:400},        
							       /* {field:'date',title:'日期',width:60},        
							        {field:'partAName',title:'委托单位',width:60}*/
									]];
	//var array_keywordFieldName=['contract.sn','contract.name'];

	var obj_initRightDatagridRow=new Object();
	$.extend(true,obj_initRightDatagridRow,obj_contractData);

	var fun_okCall=callFuncOfContractDlg;

	var array_customFun=null;

	ldrdSelect(
		str_contentDivId,
		str_idPrefix,
		str_dlgTitle,
		obj_dataGridParameter,
		obj_filterClass,
		obj_initRightDatagridRow,
		fun_okCall,array_customFun);
}

function getArrayOfContractId(){
	var array_contractDataRows=$('#contract-grid').datagrid('getRows');
	var array_contractId=new Array();
	$.each(array_contractDataRows,function(){
		array_contractId.push(this.id);
	});

	return array_contractId;
}

function callFuncOfContractDlg(array_datagridRow){
	console.log(array_datagridRow);
	var array_contractId=new Array();
	$.each(array_datagridRow,function(){
		array_contractId.push(this.id);
	});
	console.log(array_contractId);
	updateLinkWithContract(array_contractId);
};